/**
 * 新增权限
 * @url admin/system/permission/sys/add
 * @description 新增权限
 */
module.exports = {
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: '新增成功' };
		
		// 业务逻辑开始-----------------------------------------------------------
		let {
			permission_id,
			permission_name,
			comment = ''
		} = data;
		
		// 参数验证
		if (!permission_id) {
			return { code: -1, msg: '权限ID不能为空' };
		}
		if (!permission_name) {
			return { code: -1, msg: '权限名称不能为空' };
		}
		
		// 检查权限ID是否已存在
		let existPermission = await db.collection('uni-id-permissions')
			.where({ permission_id })
			.get();
		if (existPermission.data.length > 0) {
			return { code: -1, msg: '权限ID已存在' };
		}
		
		// 创建权限
		await db.collection('uni-id-permissions').add({
			permission_id,
			permission_name,
			comment,
			create_date: Date.now()
		});
		
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}

